package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.RegimenDAO;
import com.bit.cfdi.web.domain.Regimen;
@Repository
public class RegimenDAOImpl extends HibernateDaoSupport implements RegimenDAO {

	@Autowired public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional public Regimen getByDescription(String nombreRegimen) throws DataAccessException {
		List<Regimen> regimen = getHibernateTemplate().find(
				"from Regimen as r where r.descripcion = ?", nombreRegimen);
		return !regimen.isEmpty()  ?  regimen.get(0) : null;
	}

	@Override 
	@Transactional public void save(Regimen regimen) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(regimen);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<Regimen> getAll()  throws DataAccessException {
		List<Regimen> listaRegimen = getHibernateTemplate().find(				
				"from Regimen");		

		return listaRegimen;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<Regimen> getByPersonaFiscal(
			String personaFiscal) throws DataAccessException {
		List<Regimen> listaRegimen = getHibernateTemplate().find(				
		"from Regimen as regimen where regimen.personaFiscal=?", 
		personaFiscal);		

		return listaRegimen;
	}

}